package com.guigu.qingcheng.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbAlbumMapper;
import com.guigu.qingcheng.pojo.TbAlbum;
import com.guigu.qingcheng.service.TbAlbumService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
*
* @author Jiang
* @date 2020/07/25
*/
@Service
public class TbAlbumServiceImpl implements TbAlbumService {

    @Autowired(required = false)
    private TbAlbumMapper tbAlbumMapper;



    @Override
    public List<TbAlbum> queryAll() {
        List<TbAlbum> tbAlbum = tbAlbumMapper.selectAll();
            return tbAlbum;
    }

    @Override
    public PageUtil<TbAlbum> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbAlbum> pages = (Page<TbAlbum>) tbAlbumMapper.selectAll();
        PageUtil<TbAlbum> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbAlbum> pageQueryByAll(Integer pageNum, Integer pageSize, TbAlbum tbAlbum) {

        Example example=new Example(TbAlbum.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        if(!StringUtils.isEmpty(tbAlbum.getId())){
            criteria.andEqualTo("id",tbAlbum.getId());
        }
        if(!StringUtils.isEmpty(tbAlbum.getTitle())){
           criteria.andLike("title",tbAlbum.getTitle());
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<TbAlbum> pages = (Page<TbAlbum>) tbAlbumMapper.selectByExample(example);
        PageUtil<TbAlbum> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbAlbum(TbAlbum tbAlbum) {
        int insert = tbAlbumMapper.insert(tbAlbum);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbAlbum(TbAlbum tbAlbum) {
        int i = tbAlbumMapper.updateByPrimaryKeySelective(tbAlbum);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbAlbumMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateAlbumList(Map map) {
        TbAlbum album = tbAlbumMapper.selectByPrimaryKey(map.get("id"));
        String uid = UUID.randomUUID().toString();
        String url = (String) map.get("url");
        Map<String,String> map1 = new HashMap();
        map1.put("uid",uid);
        map1.put("url",url);
        map1.put("status","success");
        List<Map<String, String>> list = (List<Map<String, String>>) JSONArray.parse(album.getImageItems());
        list.add(map1);
        album.setImageItems(list.toString());
        int i = tbAlbumMapper.updateByPrimaryKeySelective(album);
        if (i>0)
            return true;
        return false;
    }

    //第一种直接判断两相册，相同不换
    //第二种根据下表去删去加
    //第三种去重
    //前端否定
    //将对象拿出来，相册相同后附加
    @Override
    public boolean removeAlbum(Map map) {
        TbAlbum album = tbAlbumMapper.selectByPrimaryKey(map.get("id"));
        List<Map<String, String>> list = (List<Map<String, String>>) JSONArray.parse(album.getImageItems());
        Map map1 = new HashMap();
        for (Map m:list) {
            if (m.get("uid").equals(map.get("uid"))){
                TbAlbum title = tbAlbumMapper.selectByPrimaryKey(map.get("title"));
                List<Map<String, String>> items =(List<Map<String, String>>) JSONArray.parse(title.getImageItems());
                items.add(m);
                title.setImageItems(items.toString());
                tbAlbumMapper.updateByPrimaryKeySelective(title);
                map1 = m;
                list.remove(m);
            }
        }
            if (map.get("id").equals(map.get("title")))
                list.add(map1);
           album.setImageItems(list.toString());
           int i = tbAlbumMapper.updateByPrimaryKeySelective(album);
           if (i>0)
               return true;
           return false;
    }

    @Override
    public boolean removeAlbumList(Map map) {
        String uids = map.get("uid").toString();
        String[] split = uids.split(",");
        boolean b = false;
        for (String uid:split) {
            Map map1 = new HashMap();
            map1.put("id",map.get("id"));
            map1.put("title",map.get("title"));
            map1.put("uid",uid);
            b = removeAlbum(map1);
        }
        return b;
    }

    @Override
    public boolean deleteAlbum(Map map) {
        TbAlbum album = tbAlbumMapper.selectByPrimaryKey(map.get("id"));
        List<Map<String, String>> list = (List<Map<String, String>>) JSONArray.parse(album.getImageItems());
        Map map1 = new HashMap();
        for (Map m:list) {
            if (m.get("uid").equals(map.get("uid"))){
                map1 = m;
            }
        }
        list.remove(map1);

        album.setImageItems(list.toString());
        int i = tbAlbumMapper.updateByPrimaryKeySelective(album);
        if (i>0)
            return true;
        return false;
    }

    @Override
    public boolean deleteAlbumList(Map map) {
        String uids = (String) map.get("uid");
        String[] split = uids.split(",");
        boolean b = false;
        for (String uid:split) {
            Map<String,String> map1 = new HashMap<>();
            map1.put("id", (String) map.get("id"));
            map1.put("uid",uid);
            b = deleteAlbum(map1);
        }
        return b;
    }

    @Override
    public TbAlbum findById(Integer id) {
        TbAlbum tbAlbum = tbAlbumMapper.selectByPrimaryKey(id);
        return tbAlbum;
    }
}
